package com.baizhi.dao.impl;

import com.baizhi.dao.UserDAO;
import com.baizhi.entity.User;
import com.baizhi.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDAOImpl implements UserDAO {

    @Override
    public User selectUserByNameAndPassword(String name, String password) {
        Connection conn = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        try{
            conn = JDBCUtil.getConnection();
            String sql = "select * from t_user where username=? and password=?";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1,name);
            pstm.setString(2,password);
            rs = pstm.executeQuery();
            if(rs.next()){
                user = new User();
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
            }
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally {
            JDBCUtil.close(conn,pstm,rs);
        }
        return user;
    }
}
